//
//  ViewController.swift
//  CameraDemo
//
//  Created by 劢克科技 on 2019/5/14.
//  Copyright © 2019 meckey. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    let imagePicker = UIImagePickerController()

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    @IBAction func handleClick() {
        // 验证相机是否可用
        if !isCameraAvailable() {
            print("相机不可用")
            return
        }

        guard let avaliableType = UIImagePickerController.availableMediaTypes(for: .camera) else {
            print("相机无法拍照")
            return
        }
        
        imagePicker.mediaTypes = avaliableType                  // 选择拍照或视频
        imagePicker.sourceType = .camera                        // 选择调用相机
        imagePicker.delegate = self                             // 选择代理
        present(imagePicker, animated: true, completion: nil)
    }
    
    
    /// 验证相机是否可用
    ///
    /// - Returns: True：可以使用；False：不能使用相机
    fileprivate func isCameraAvailable() -> Bool {
        let isCameraAvailable =  UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.camera)
        return isCameraAvailable
    }
}

//MARK: - < ImagePicker Delegate >

extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        imagePicker.dismiss(animated: true, completion: nil)
        
        let type = info[UIImagePickerController.InfoKey.mediaType] as! String
        
        if type.contains("image") {
            // 拍照片将照片保存到本地
            let image = info[UIImagePickerController.InfoKey.originalImage]
            UIImageWriteToSavedPhotosAlbum(image as! UIImage, nil, nil, nil)
        } else if type.contains("movie") {
            // 录视频
            let url = info[UIImagePickerController.InfoKey.mediaURL] as! URL
            
            URLSession.shared.dataTask(with: url) { (data, response, err) in
                guard let data = data else {
                    print("保存失败")
                    return
                }
                guard let path = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first else {
                    print("本地路径获取失败")
                    return
                }
                let videoPath = (path as NSString).appendingPathComponent("saveVideo.mp4")
                let url = URL(fileURLWithPath: videoPath)
                (data as NSData).write(to: url, atomically: true)
                
                UISaveVideoAtPathToSavedPhotosAlbum(videoPath, nil, nil, nil)
                DispatchQueue.main.async {
                    print("保存成功")
                }
                }.resume()
        }
    }
}
